package com.systemframework.sys.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.systemframework.annotation.LicenseAnnotation;
import com.systemframework.annotation.LogAnnotation;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.sys.dto.request.SysDeptRequestDTO;
import com.systemframework.sys.dto.response.SysDeptResponseDTO;
import com.systemframework.sys.entity.SysDept;
import com.systemframework.sys.service.SysDeptService;
import com.systemframework.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @date 2023-03-21 9:34
 */
@RestController
@RequestMapping(value = "/dept")
@Slf4j
public class SysDeptController implements BaseCommonInterface<SysDeptRequestDTO, SysDeptResponseDTO> {

    private SysDeptService sysDeptService;

    @Autowired
    public void setSysDeptService(SysDeptService sysDeptService) {
        this.sysDeptService = sysDeptService;
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.INSERT,name = "部门信息")
    @Override
    public Result<String> save(SysDeptRequestDTO sysDeptRequestDTO) {
        SysDept dept = Convert.convert(SysDept.class, sysDeptRequestDTO);
        sysDeptService.save(dept);
        return Result.successResult();
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "部门信息")
    @Override
    public Result<String> update(SysDeptRequestDTO sysDeptRequestDTO) {
        SysDept dept = Convert.convert(SysDept.class, sysDeptRequestDTO);
        sysDeptService.updateById(dept);
        return Result.successResult();
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.DELETE,name = "部门信息")
    @Override
    public Result<String> del(Integer id) {
        sysDeptService.removeById(id);
        return Result.successResult();
    }

    @LicenseAnnotation
    @Override
    public Result<Page<SysDeptResponseDTO>> lists(SysDeptRequestDTO sysDeptRequestDTO) {
        return null;
    }

    @LicenseAnnotation
    @Override
    public Result<SysDeptResponseDTO> findById(Integer id) {
        SysDept sysDept = sysDeptService.getById(id);
        return Result.successResult(Convert.convert(SysDeptResponseDTO.class,sysDept));
    }

    @LicenseAnnotation
    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "部门信息")
    @Override
    public Result<String> batchDel(List<SysDeptResponseDTO> lists) {
        List<Integer> ids = lists.stream().map(SysDeptResponseDTO::getId).collect(Collectors.toList());
        if(ids.size()>0){
            sysDeptService.removeByIds(ids);
        }
        return Result.successResult();
    }

    @LicenseAnnotation
    @PostMapping(value = "/findTreeLists")
    public Result<List<SysDeptResponseDTO>> findTreeLists(@RequestBody SysDeptRequestDTO sysDeptRequestDTO){
        return Result.successResult(sysDeptService.childrenDeptLists(sysDeptRequestDTO));
    }


}
